package com.t235.gc.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.t235.gc.AjaxResult;
import com.t235.gc.entity.Goods;
import com.t235.gc.goods.service.DeviceCabinetService;
import com.t235.gc.goods.service.GoodsService;
import com.t235.gc.goodsvo.DeviceCabinetUpdateVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

//@CrossOrigin
@Api
@RestController
@RequestMapping("/api/goods/admin")
public class GoodsController {
    @Autowired
    DeviceCabinetService deviceCabinetService;
    @Autowired
    GoodsService goodsService;


    @GetMapping("/getBackGoodsList")
    public AjaxResult FindByGoodsAndDeviceVo(Goods goods, Integer page, Integer limit, HttpSession session){
        System.out.println("sid="+session.getId());
        //System.out.println(isstock+"   "+isfault);
        //isstock是否缺货 isfault是否故障
        //查询所有商品数据
//        return AjaxResult.success(goodsService.FindByAll(goods));
        PageHelper.startPage(page,limit);
        List<Goods> goodsList = goodsService.FindByAll(goods);
        PageInfo<Goods> pages=new PageInfo<>(goodsList);
        List<Goods> list = pages.getList();
        long total = pages.getTotal();
        return AjaxResult.success(list,total);
    }
    @DeleteMapping("/delGoods")
    public AjaxResult delGoods(Long goodsid){
        //根据id删除商品
        if(goodsService.deleteByPrimaryKey(goodsid)>0){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }
    @PostMapping("/updateIsOff")
    public AjaxResult updateIsOff(Goods goods){
        //上架与下架
        Integer num=goods.getIsoff()==1 ? 2 : 1;
        goods.setIsoff(num);
        if(goodsService.updateByIsoff(goods)>0){
            return AjaxResult.success("上下架成功");
        }else{
            return AjaxResult.error("上下架失败!");
        }
    }
    @PostMapping("/saveOrUpdateGoods")
    public AjaxResult updateByPrimaryKeySelective(Goods goods,MultipartFile file1) throws IOException {
        //根据商品id 修改商品类型于名称
        if (file1.isEmpty()) {
            return AjaxResult.error("添加商品失败,没有选中文件");
        }
        String filename= UUID.randomUUID().toString()+file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
        String path = "F:/purl/"+filename;
        String location=path.replace("\\\\", "/");
        //创建文件路径
        File dest = new File(path);
        goods.setGoodspic("uploadImgs/"+filename);
        file1.transferTo(dest);
        System.out.print("保存文件路径"+path+"\n");
        if(goodsService.updateByPrimaryKeySelective(goods)>0){
            return AjaxResult.success("修改商品成功");
        }else{
            return AjaxResult.error("修改商品失败");
        }
    }
    @PostMapping("/saveOrAddGoods")
    public AjaxResult AddAndGoods(Goods goods, MultipartFile file1, HttpServletRequest request) throws IOException {
        //添加商品

        if (file1.isEmpty()) {
            return AjaxResult.error("添加商品失败,没有选中文件");
        }
        String filename= UUID.randomUUID().toString()+file1.getOriginalFilename().substring(file1.getOriginalFilename().lastIndexOf("."));
        String path = "F:/purl/"+filename;
        String location=path.replace("\\\\", "/");
        //创建文件路径
        File dest = new File(path);
        goods.setGoodspic("uploadImgs/"+filename);
        file1.transferTo(dest);
        System.out.print("保存文件路径"+path+"\n");
        goods.setIsdel(1);
        goods.setIsoff(1);
        if(goodsService.insertSelective(goods)>0){
            return AjaxResult.success("添加商品成功");
        }else{
            return AjaxResult.error("添加商品失败");
        }
    }
}
